<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="UTF-8">
<title>Testing websockets</title>
</head>
<body>
  <div>
    <input id="inputmessage" type="text" />
  </div>
  <div>
    <input type="submit" value="Broadcast message" onclick="send()" />
    <input type="submit" value="Close socket" onclick="stop()" /><br/>
    <input type="submit" value="Close http session" onclick="closeHttpSession()" />
  </div>

  <br/>
  <div>
  <input type="submit" value="Add message as lastUser session attribute" onclick="addSessionAttr()" />
  
  <a href="gameAddSessionAttribute.jsp?user=someUsername">Add session attribute</a>
  <script type="text/javascript">
  function addSessionAttr() {
	  var txt = document.getElementById('inputmessage').value;
	  window.location.href = 'gameAddSessionAttribute.jsp?user=someUsername'+txt.substring(0,100);
  }
  function closeHttpSession() {
	  window.location.href = 'DestroySession.jsp';
  }
  </script>
  </div>
  

<%@page import="java.util.Map"%>
<%
	//session.setAttribute("counter",11);
	
	Object attrib = session.getAttribute("countOfSession");
	String cnt = "0";
	if (attrib!=null) {
		cnt= attrib.toString();
	}
	out.print("cnt: " + cnt + "</br>");
	this.log("countOfSession: "+cnt);
	
	
	Map<String, HttpSession> httpSessionsMap;
	attrib = session.getAttribute("httpSessionsMap");
	if (attrib!=null){
		out.print("sessionsMap: " + attrib + "</br>");
	}
	
	attrib = session.getAttribute("gamePortalData");
	if (attrib!=null){
		out.print("gamePortalData: " + attrib + "</br>");
	}
%>
  countOfSession: ${countOfSession}
  <br/>
  <br/>
  <div id="messages"></div>
  <script type="text/javascript">
    var webSocket = 
      new WebSocket('ws://localhost:8180/duelgame/websocket-game');

    webSocket.onerror = function(event) {
      onError(event)
    };

    webSocket.onopen = function(event) {
      onOpen(event)
    };

    webSocket.onmessage = function(event) {
      onMessage(event)
    };

    function onMessage(event) {
      document.getElementById('messages').innerHTML 
        += '<br />Received message: ' + event.data;
    }

    function onOpen(event) {
      document.getElementById('messages').innerHTML 
        = 'Connection established';
    }

    function onError(event) {
      alert(event.data);
    }

    function send() {
      var txt = document.getElementById('inputmessage').value;
      webSocket.send(txt);
      document.getElementById('messages').innerHTML 
      += '<br />Sended message: ' + txt;
      return false;
    }

    function stop() {
        webSocket.close();
        document.getElementById('messages').innerHTML 
        += '<br />' + 'Connection closed';
        return false;
    }
    </script>
</body>
</html>